import { Color } from "three";

const RainShader = {
    uniforms: {
        'tDiffuse': { value: null },
        'iResolution': { value: null },
        'iTime': { value: 0 },
        'radian': { value: 0.25 },
        'rainSpeed': { value: 0.2 },
        'rainColor': { value: new Color(1, 1, 1) }
    },

    vertexShader: /* glsl */`

        varying highp vec2 vUv;

            void main() {

                vUv = uv;
                gl_Position = projectionMatrix * modelViewMatrix * vec4( position, 1.0 );

        }`,
    fragmentShader: `
         uniform sampler2D tDiffuse;
         varying highp vec2 vUv;
         uniform float iTime;
         uniform vec2 iResolution;
     
         float hash(float x){
             return fract(sin(x*133.3)*13.13);
         }
         
         void main(void){
             float time = iTime / 2.;
         
             //vec2 uv=(gl_FragCoord.xy*2.-resolution.xy)/min(resolution.x,resolution.y);
             vec2 uv = gl_FragCoord.xy/iResolution.xy;
          
             vec3 c=vec3(.6,.7,.8);
         
             float a=-.1;
             float si=sin(a),co=cos(a);
             uv*=mat2(co,-si,si,co);
             uv*=length(uv+vec2(0,4.9))*.3+1.;
         
             float v=1.-sin(hash(floor(uv.x*200.))*2.);
             float b=clamp(abs(sin(20.*time*v+uv.y*(5./(2.+v))))-.97,0.,1.)*20.;
             c*=v*b; //屏幕上雨的颜色
         
             gl_FragColor = mix(vec4(texture2D(tDiffuse,vUv).rgb,1.0), vec4(c,1), 0.5); //将雨和三维场景融合
         }
        `,

    fragmentShader1: /* glsl */`
        uniform sampler2D tDiffuse;
        uniform vec2 iResolution;
        uniform float iTime;
        uniform float radian;
        uniform float rainSpeed;
        uniform vec3 rainColor;
        varying highp vec2 vUv;

        float random(float t){
           return fract(sin(t*745.523)*7894.552);
        }
        
        void main(){
            vec3 col=texture2D(tDiffuse,vUv).rgb;
            vec2 uv = gl_FragCoord.xy/iResolution.xy;
            mat2 r=mat2(cos(radian),-sin(radian),sin(radian),cos(radian));
            uv*=r;
            uv.y += iTime*4.0*rainSpeed;
            uv.xy *= 100.0;
            uv.y += random(floor(uv.x))*100.0;
            vec3 rain=vec3(clamp(1.0-length(vec2(cos(uv.x*3.1415 ), sin(uv.y*0.2)-1.7)), 0.0,1.0))*rainColor;
            gl_FragColor = vec4(col+rain,1.0);
        }`

};

export { RainShader };